from django.db import models
from user.models import BaseModel
from user.models import User


class Rule(BaseModel):
    name = models.CharField(max_length=100)
    point = models.IntegerField()
    type = models.BooleanField(verbose_name='扣分或加分，false为扣分，true为加分')

    class Meta:
        db_table = 'rule'


class Behavior(BaseModel):
    break_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='behavior', verbose_name='违纪人')
    rule = models.ForeignKey(Rule, on_delete=models.CASCADE, related_name='behavior', verbose_name='违纪类型')
    be_time = models.DateTimeField(verbose_name='违纪时间', auto_now_add=True)
    description = models.TextField(verbose_name='违纪描述')
    be_place = models.TextField(verbose_name='违纪地点')

    class Meta:
        db_table = 'behavior'

    def to_json(self):
        return {
            'id': self.id,
            'break_user': self.break_user.username,
            'rule': self.rule.name,
            'be_time': str(self.be_time),
            'description': self.description,
            'be_place': self.be_place,
        }
